//
//  AddModuleCollectionViewCell.m
//  ZhuoxinAgriculture
//
//  Created by stone on 16/11/13.
//  Copyright © 2016年 KKING. All rights reserved.
//

#define HexRGBA(rgbValue, al) [UIColor colorWithRed:((float)((rgbValue & 0xFF0000) >> 16)) / 255.0 green:((float)((rgbValue & 0xFF00) >> 8)) / 255.0 blue:((float)(rgbValue & 0xFF)) / 255.0 alpha:al]

#define HexRGB(rgbValue) HexRGBA(rgbValue, 1.0)
#import "AddModuleAlreadyAddedCollectionViewCell.h"
#import "ConstValues.h"
@interface AddModuleAlreadyAddedCollectionViewCell()
@property (weak, nonatomic) UIButton *deleteButton;
@property (weak, nonatomic) UILabel *titleLabel;
@property (weak, nonatomic) UILabel *subTitleLabel;
@property (weak, nonatomic) UIImageView *iconImageView;
@end
@implementation AddModuleAlreadyAddedCollectionViewCell


- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self addViews];
        [self setupUI];
        self.contentView.backgroundColor = [UIColor whiteColor];
    }
    return self;
}
- (void)addViews
{
    UILabel * titleLabel = ({
        UILabel * label = [[UILabel alloc] init];
        /** GPU 优化 */
        //        label.opaque = YES;
        //        label.backgroundColor = [UIColor whiteColor];
        //        label.layer.masksToBounds = YES;
        //------------------------------
        label.numberOfLines = 0;
        label.textColor = [UIColor blackColor];
        label.textAlignment = NSTextAlignmentCenter;
        label.text = @"Label的默认内容";
        label.font = [UIFont systemFontOfSize:12];
        
        [self.contentView addSubview:label];
        
        label;
    });
    self.titleLabel = titleLabel;
    UILabel * subTitleLabel = ({
        UILabel * label = [[UILabel alloc] init];
        /** GPU 优化 */
        //        label.opaque = YES;
        //        label.backgroundColor = [UIColor whiteColor];
        //        label.layer.masksToBounds = YES;
        //------------------------------
        label.numberOfLines = 0;
        label.textColor = [self colorWithString:@"7d7c82"];
        label.textAlignment = NSTextAlignmentCenter;
        label.text = @"Label的默认内容";
        label.font = [UIFont systemFontOfSize:9];
        
        [self.contentView addSubview:label];
        
        label;
    });
    self.subTitleLabel = subTitleLabel;
    
    UIImageView * iconImageView = ({
        UIImageView * imageView = [[UIImageView alloc] init];
        
        [self.contentView addSubview:imageView];
        
        imageView;
    });
    self.iconImageView = iconImageView;
    
    
    UIButton * deleteButton = ({
        UIButton * button = [[UIButton alloc] init];
        
        [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [button setBackgroundImage:[UIImage imageNamed:@"圆角矩形 1 拷贝 2"] forState:UIControlStateNormal];
        
        // [button setImage:[UIImage imageNamed:@"normalImage"] forState:UIControlStateNormal];
        // [button setImage:[UIImage imageNamed:@"selectedImage"] forState:UIControlStateSelected];
        
        [button addTarget:self action:@selector(deleteButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
        
        
        [self.contentView addSubview:button];
        
        
        
        button;
    });
    
    self.deleteButton = deleteButton;
    
    
    
}
- (CGFloat)calculateLabelHeight:(UILabel *)label
{
    CGSize size = [[[NSAttributedString alloc] initWithString:label.text attributes:@{NSFontAttributeName : label.font}] boundingRectWithSize:CGSizeMake(self.frame.size.width, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin context:nil].size;
    return size.height;
}
- (void)setupUI
{
    
    self.deleteButton.frame = CGRectMake(self.frame.size.width - kDeleteButtonHeight-5, 5, kDeleteButtonHeight, kDeleteButtonHeight);
    self.titleLabel.frame = CGRectMake(0, 15, self.frame.size.width, [self calculateLabelHeight:self.titleLabel]);
    self.subTitleLabel.frame = CGRectMake(0, CGRectGetMaxY(self.titleLabel.frame)+5, self.frame.size.width, [self calculateLabelHeight:self.subTitleLabel]);
    
    CGFloat y = CGRectGetMaxY(self.subTitleLabel.frame) + (self.frame.size.height - CGRectGetMaxY(self.subTitleLabel.frame) - kImageViewHeight) * 0.5;
    NSLog(@"y = %f",y);
    self.iconImageView.frame = CGRectMake(0, y, self.frame.size.width, kImageViewHeight);
}

-(void)setModel:(DBModel *)model{
    _model = model;
    self.titleLabel.text = model.title;
    self.subTitleLabel.text = model.subTitle;
    self.iconImageView.image = [UIImage imageNamed:model.iconName];
    self.titleLabel.textColor = [self colorWithString:model.titleColorString];
    CGFloat w = self.iconImageView.frame.size.height * self.iconImageView.image.size.width / self.iconImageView.image.size.height;
    self.iconImageView.frame = CGRectMake((self.frame.size.width - w) * 0.5, self.iconImageView.frame.origin.y, self.iconImageView.frame.size.height * self.iconImageView.image.size.width / self.iconImageView.image.size.height, self.iconImageView.frame.size.height);
}

- (void)deleteButtonClicked:(UIButton *)sender {
    NSLog(@"%@ | %s",self.class,__func__);
    self.model.addition = NO;
    [self.notAdded addObject:self.model];
    [self.alreadyAdded removeObject:self.model];
    
    if ([self.delegate respondsToSelector:@selector(refreshCollectionView)]) {
        [self.delegate refreshCollectionView];
    }
}
//sn_note:=========  ============================ stone 🐳 ===========/
/** 16进制(字符串形式) 返回 图片 */
- (UIImage *)imageWithString:(NSString *)string {
    
    return [self imageWithColor:[self colorWithString:string]];
}
/** 16进制返回颜色 */
- (UIColor *)colorWithString:(NSString *)string {
    CGFloat alpha = 1.0f;
    NSNumber * red = @0.0;
    NSNumber * green = @0.0;
    NSNumber * blue = @0.0;
    
    NSMutableArray<NSNumber *> * colors = [NSMutableArray arrayWithArray:@[ red, green, blue ]];
    
    unsigned hexComponent;
    NSString * colorString = [string uppercaseString];
    NSLog(@"%ld", colors.count);
    for (int i = 0; i < colors.count; i++) {
        NSString * substring = [colorString substringWithRange:NSMakeRange(i * 2, 2)];
        
        [[NSScanner scannerWithString:substring] scanHexInt:&hexComponent];
        
        NSNumber * temp = colors[i];
        temp = @(hexComponent / 255.0);
        colors[i] = temp;
    }
    
    return [UIColor colorWithRed:[colors[0] floatValue] green:[colors[1] floatValue] blue:[colors[2] floatValue] alpha:alpha];
}
/** 颜色 返回 图片 */
- (UIImage *)imageWithColor:(UIColor *)color {
    
    CGRect rect = CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    
    CGContextSetFillColorWithColor(context, [color CGColor]);
    CGContextFillRect(context, rect);
    
    UIImage * image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    return image;
}

@end
